package com.lh.web.controller;

import com.lh.common.entity.admin.AppVersion;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import com.lh.web.service.AppVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @author FH
 * @ClassName: AppVersionController
 * @Description: (类作用)
 * @date: 2020-06-24 9:46
 */
@RestController
@Api(value="AppVersionAction",tags={"App版本管理"})
@RequestMapping("app")
public class AppVersionController extends AbstractController {

    @Autowired
    private AppVersionService appVersionService;

    /**
     * @Title: appVersionList
     * @Description: 更新App的版本
     * @return Result    返回类型
     * @throws
     */
    @ApiOperation(value = "app版本列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class)
    })
    @GetMapping("/appVersionList")
    @ResponseBody
//    @RequiresPermissions("app:appVersionList")
    public Result<AppVersion> appVersionList(Integer pageNum, Integer pageSize) {
        PageUtils<AppVersion> pageUtils = appVersionService.queryAppVersionList(pageNum, pageSize);
        return Result.ok(pageUtils);
    }

    /**
     * @Title: addAppVersion
     * @Description: 新增/修改app版本
     * @return Result    返回类型
     * @throws
     */
    @ApiOperation(value = "新增/修改app版本")
    @PostMapping("/addAppVersion")
    @ResponseBody
//    @RequiresPermissions("app:addAppVersion")
    public Result addAppVersion(@RequestBody AppVersion appVersion) {
        AssertUtil.assertNotNull(appVersion.getChannel(), Constant.RESULT.FI1000, "发布渠道不允许为空");
        AssertUtil.assertNotNull(appVersion.getForceUpdate(), Constant.RESULT.FI1000, "升级方式不允许为空");
        AssertUtil.assertNotNull(appVersion.getVersion(), Constant.RESULT.FI1000, "版本号不允许为空");
        AssertUtil.assertNotNull(appVersion.getApkUrl(), Constant.RESULT.FI1000, "版本下载链接不允许为空");
        AssertUtil.assertNotFalse(Pattern.matches("v\\d\\.\\d\\.\\d", appVersion.getVersion()), Constant.RESULT.FI1000, "版本号格式错误，形如:v1.0.0");
        AssertUtil.assertNotTrue(StringUtils.isNotEmpty(appVersion.getUpdateInfo())&&appVersion.getUpdateInfo().length()>200, Constant.RESULT.FI1000, "发布信息最长200个字符");
        AssertUtil.assertNotTrue(StringUtils.isNotEmpty(appVersion.getRemark())&&appVersion.getRemark().length()>200, Constant.RESULT.FI1000, "备注最长200个字符");
        if(appVersion.getId() == null) {
            int versionCode = 0;
            try {
                if(StringUtils.isNotEmpty(appVersion.getVersion())){
                    versionCode = Integer.valueOf(appVersion.getVersion().replaceAll("\\.","").replaceAll("v",""));
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            appVersion.setVersionCode(versionCode);
            appVersionService.insertAppVersion(appVersion);
            return Result.ok(appVersion);
        }
        appVersionService.updateAppVersion(appVersion);
        return Result.ok(appVersion);
    }

    /**
     * @Title: delAppVersion
     * @Description: 删除app版本
     * @return Result    返回类型
     * @throws
     */
    @ApiOperation(value = "删除app版本")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "ids", value = "app版本id多个", required = true, dataTypeClass= Long.class)
    })
    @GetMapping("/delAppVersion")
    @ResponseBody
//    @RequiresPermissions("app:delAppVersion")
    public Result delAppVersion(Long[] ids) {
        try {
            appVersionService.deleteAppVersion(ids);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("删除app版本异常!");
        }
    }
}
